package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.api.business.model.param.mapArea.MapAreaActionParams;
import com.hitqz.robot.api.business.model.vo.mapArea.AreaActionVo;
import com.hitqz.robot.api.business.model.vo.mapArea.MapAreaActionVo;
import com.hitqz.robot.api.common.dto.tickets.CtrlTicketsEvent;
import com.hitqz.robot.api.common.entity.*;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.AreaActionService;
import com.hitqz.robot.biz.service.ButtonService;
import com.hitqz.robot.biz.service.MapAreaActionService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.dto.QueryCameraParamDTO;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;

/**
 * @author xupkun
 * @date 2024/6/3
 */
@RestController
@AllArgsConstructor
@RequestMapping("/button")
@Tag(name = "按钮管理")
public class ButtonController {

    private final ButtonService buttonService;

    private final RateLimiter abortLimiter = RateLimiter.create(1);


    @GetMapping("/list")
    @Operation(summary = "获取按钮列表")
    public R<List<Button>> list() {
        return R.ok(buttonService.list());
    }


    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询获取按钮")
    public R<Button> getById(@PathVariable("id") Integer id) {
        return R.ok(buttonService.getById(id));
    }



    @PostMapping
    @Operation(summary = "添加按钮")
    public R save(@RequestBody Button button) {
        buttonService.save(button);
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "更新按钮")
    public R  update(@RequestBody Button button) {
        buttonService.updateById(button);
        return R.ok();
    }


    @DeleteMapping
    @Operation(summary = "删除按钮")
    public R delete(@RequestBody Integer[] ids) {
        buttonService.removeBatchByIds(CollectionUtil.newArrayList(ids));
        return R.ok();
    }

    @GetMapping("/reset")
    @Operation(summary = "恢复出厂设置")
    public R reset() {
        // 删除全部
        buttonService.remove(Wrappers.<Button>lambdaQuery());
        // 重新添加默认按钮
        buttonService.init();
        return R.ok();
    }


    @GetMapping("/motionAbort")
    @Operation(summary = "全局急停按钮")
    public R motionAbort() {
        //限流
        if (abortLimiter.tryAcquire()){
            // 发送全局急停指令
            CtrlTicketsEvent ctrlTicketsEvent = new CtrlTicketsEvent();
            ctrlTicketsEvent.setType(CtrlTicketsEvent.TYPE_PAUSE);
            ctrlTicketsEvent.setMessage("上位机急停,任务暂停");
            ctrlTicketsEvent.setTicketsId(LiteFlowUtil.ticketsId);
            SpringUtil.getApplicationContext().publishEvent(ctrlTicketsEvent);
        }
        return R.ok();
    }



}
